package wangm.nettyhttp;

import wangm.nettyhttp.channelhandler.HttpServerInitializer;
import io.netty.bootstrap.ServerBootstrap;
import io.netty.channel.ChannelFuture;
import io.netty.channel.EventLoopGroup;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.nio.NioServerSocketChannel;

import java.net.InetSocketAddress;

/**
 * describe
 *
 * @author zk
 * @date 2018-02-09 14:08
 */
public class HttpServer {
    public static void main(String[] args) throws InterruptedException {
        new HttpServer().start();
    }

    public void start() throws InterruptedException {
        EventLoopGroup group = new NioEventLoopGroup();
        try {
            ServerBootstrap b = new ServerBootstrap();
            b.group(group)
                    .channel(NioServerSocketChannel.class)
                    .localAddress(new InetSocketAddress(8092))
                    .childHandler(new HttpServerInitializer());
            ChannelFuture f = b.bind().sync();
            System.out.println("post at :" + 8092);
            f.channel().closeFuture().sync();
        } catch (Exception e) {
            e.printStackTrace();
        }finally {
            group.shutdownGracefully().sync();
        }

    }
}
